<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="部门名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入部门名称"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="infoPhone">
              <a-input v-model="model.infoPhone" placeholder="请输入联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="办公时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="infoTime">
              <a-input v-model="model.infoTime" placeholder="请输入办公时间"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="办公地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="infoAddress">
              <a-input v-model="model.infoAddress" placeholder="请输入办公地址"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="简介图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="infoImage" >
              <j-image-upload     v-model="model.infoImage" ></j-image-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="产品图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodImage" >
              <j-image-upload     v-model="model.goodImage" ></j-image-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="app简介" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="introduction">
              <a-textarea v-model="model.introduction" rows="4" placeholder="请输入简介"/>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="pc简介" :labelCol="{xs: { span: 24 },sm: { span: 2 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 21 }}"  prop="infoContent" >
              <j-editor v-model="model.infoContent" :height=500   watermark="false" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="地图坐标" :labelCol="{xs: { span: 24 },sm: { span: 2 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 21 }}"  prop="infoLng" >
              <div style="display: none;">
                <a-col :md="12" :lg="12">
                  <a-input  v-model="model.infoLng" placeholder="请选择坐标点经度"/>
                </a-col>
                <a-col :md="12" :lg="12">
                  <a-input v-model="model.infoLat" placeholder="请选择坐标点纬度"/>
                </a-col>
              </div>
              <div style="height:400px">
                <baidu-map class="bm-view" @click="mapClick" :zoom="zoom" @ready="handler"
                           ak="DGvQswfMZ8roB4bYGBA85XyTkALDNbpW" :center="center"
                           :scroll-wheel-zoom="true" :min-zoom="min_zoom" :max-zoom="max_zoom" >
                  <bm-control :offset="{width: '10px', height: '10px'}">
                    <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 1}">
                      <a-input class="search-map"  placeholder="请输入地名关键字"/> <!-- 这里指代一个自定义搜索框组件 -->
                    </bm-auto-complete>
                  </bm-control>
                  <bm-local-search :keyword="keyword" :auto-viewport="true"></bm-local-search>
                </baidu-map>
              </div>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" >
            <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" >
                <a-button @click="submit" type="primary">提交</a-button>
                <!--  <a-button style="margin-left: 8px">保存</a-button>-->
            </a-form-model-item>
          </a-col>

        </a-row>

      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import Vue from 'vue'
  import { validateDuplicateValue } from '@/utils/util'
  import {httpAction, getAction} from '@/api/manage'
  import AvatarModal from './AvatarModal'
  import {BaiduMap, BmControl, BmAutoComplete, BmLocalSearch} from 'vue-baidu-map'
  import { USER_INFO } from "@/store/mutation-types"
  export default {
    components: {
      AvatarModal,
      BaiduMap,
      BmControl,
      BmAutoComplete,
      BmLocalSearch
    },

    data() {
      return {
        user:{
          id:"",
          orgCode:"",
          username:""
        },
        preview: {},
        model: {
          id: "",
          name: "",
          infoPhone: "",
          infoTime: "",
          infoAddress: "",
          infoContent: "",
          infoLng: "",
          infoLat: "",
          infoImage: "",
          goodImage: "",
        },
        center: {lng: 116.404, lat: 39.915},
        zoom: 10,
        min_zoom :5,
        max_zoom :100,
        keyword: '徐州',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 19 },
        },
        confirmLoading: false,
        validatorRules: {
          name: [
            { required: true, message: '请输入部门名称!'},
          ],
          infoPhone: [
            { required: true, message: '请输入联系电话!'},
          ],
          infoTime: [
            { required: true, message: '请输入办公时间!'},
          ],
          infoAddress: [
            { required: true, message: '请输入办公地址!'},
          ],
          infoContent: [
            { required: true, message: '请输入简介说明!'},
          ],
          infoImage: [
            { required: true, message: '请上传简介图片!'},
          ],
          goodImage: [
            { required: true, message: '请上传产品图片!'},
          ],
          infoLng: [
            { required: true, message: '请输入选择经度!'},
          ],
          infoLat: [
            { required: true, message: '请输入选择纬度!'},
          ],
        },
        url: {
          edit: "/cooperationunit/cooperationUnit/edit",
          get: "cooperationunit/cooperationUnit/queryById",
          getId: "cooperationunit/cooperationUnit/getId"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created() {
      this.user = Vue.ls.get(USER_INFO);
      //console.log(this.user);
      this.getData();
    },
    methods: {
      submitForm() {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            httpAction(this.url.edit, this.model, 'put').then((res) => {
              if (res.success) {
                that.$message.success(res.message);
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })


      },

      getData() {
        //先获取当前登录用户的合作机构id  查询是否有
        httpAction(this.url.getId, this.user, 'put').then((res) => {
          if (res.success) {
            //console.log(res.result)
            if(res.result.id){
              this.model.id = res.result.id;
              //根据id 获取数据详情
              getAction(this.url.get, {id: this.model.id}, 'get').then((res) => {
                if (res.success) {
                  //console.log(res.result)
                  this.model = res.result;

                } else {
                  that.$message.warning(res.message);
                }
              })
            }
          } else {
            that.$message.warning(res.message);
          }
        })

      },

      handler({BMap, map}) {
        //console.log(BMap, map)
        this.center.lng = 116.404;
        this.center.lat = 39.915;
        this.zoom = 15;
      },
      mapClick(e) {
        //console.log(e)
        this.model.infoLat = e.point.lat;
        this.model.infoLng = e.point.lng;
      },
      submit(e) {
        this.submitForm();
      }
    }
  }
</script>

<style lang="less" scoped>
  .search-map{
    width: 300px;
  }
  .bm-view {
    height: 100%;
  }

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0, 0, 0, 0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>